#  Copyright (c) 2021 Terminus, Inc.
#
#  Licensed under the Apache License, Version 2.0 (the "License");
#  you may not use this file except in compliance with the License.
#  You may obtain a copy of the License at
#
#       http://www.apache.org/licenses/LICENSE-2.0
#
#  Unless required by applicable law or agreed to in writing, software
#  distributed under the License is distributed on an "AS IS" BASIS,
#  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#  See the License for the specific language governing permissions and
#  limitations under the License.

import json

import django.db.models


class DiceRelease(django.db.models.Model):
    """
    generated by erda-cli
    """

    release_id = django.db.models.CharField(primary_key=True)
    release_name = django.db.models.CharField()
    desc = django.db.models.TextField()
    dice = django.db.models.TextField()
    addon = django.db.models.TextField()
    labels = django.db.models.CharField()
    version = django.db.models.CharField()
    org_id = django.db.models.BigIntegerField()
    project_id = django.db.models.BigIntegerField()
    application_id = django.db.models.BigIntegerField()
    project_name = django.db.models.CharField()
    application_name = django.db.models.CharField()
    user_id = django.db.models.CharField()
    cluster_name = django.db.models.CharField()
    cross_cluster = django.db.models.BooleanField()
    resources = django.db.models.TextField()
    reference = django.db.models.BigIntegerField()
    created_at = django.db.models.DateTimeField()
    updated_at = django.db.models.DateTimeField()
    changelog = django.db.models.TextField()
    is_stable = django.db.models.BooleanField()
    is_formal = django.db.models.BooleanField()
    is_project_release = django.db.models.BooleanField()
    application_release_list = django.db.models.TextField()
    tags = django.db.models.CharField()
    git_branch = django.db.models.CharField()

    class Meta:
        db_table = "dice_release"


def entry():
    releases = DiceRelease.objects.all()

    for release in releases:
        if not release.is_project_release:
            continue
        try:
            oldList = json.loads(release.application_release_list)
            newList = [oldList]
            s = json.dumps(newList)
            release.application_release_list = s
            release.save()
        except Exception as e:
            print("release %s git branch parse error, exception: %s, skip" % (release.release_id, e))


entries: [callable] = [
    entry,
]
